class CpController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, CpService, getCpList, iRunner, getDeviceList){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
    	vm.CpService 	 = CpService;
    	vm.getCpList 	 = getCpList;
    	vm.iRunner 	     = iRunner;
        vm.deviceList    = getDeviceList;

    	// 表单数据初始化
        vm.cp = {
            cp_name: '',
            cp_index: 1
        }

        // 设置默认计时点编号
        if (vm.getCpList.code == 0 && vm.getCpList.data.length) {
            vm.cp.cp_index = parseInt(vm.getCpList.data[vm.getCpList.data.length - 1].cp_index) + 1;
        } else {
            vm.cp.cp_index = 1;
        }
        // vm.getCpList.code==0&&(vm.cp.cp_index = vm.getCpList.data.length[vm.getCpList.data.length - 1] + 1);

        //已绑定设备列表
        vm.bindEquipmentList = [];

        //待绑定设备列表
        vm.list = [];
        for(var key in vm.deviceList.data){
           vm.list.push(vm.deviceList.data[key]);//往数组中放属性
        }
        vm.equipmentList = vm.list;


        //向下拉框中添加待绑定设备
        angular.forEach(vm.equipmentList, function (element, index) {
            $("#select_id").append("<option value='" + element.mid + "'>" + element.title + "</option>"); 
        });

        var config = {
            '.chosen-select'           : {},
            '.chosen-select-deselect'  : {allow_single_deselect:true},
            '.chosen-select-no-single' : {disable_search_threshold:10},
            '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'}
                    }
        for (var selector in config) {
            $(selector).chosen(config[selector]);
        }

        // 获取123赛事id
        vm.CpService.getTrelated(vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
        .then( data => {
            if (data.code == 0) {
                vm.camera_id = data.data.related_id;
            }
            // 调用失败, 提示用户
            (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
            // 登录信息已过期, 请重新登录
            data.code==-1001&&vm.dialogService.error(data.message, true);
        }, error => console.log('error'));

        // 创建摄像点
        vm.create_camera_id = function(id) {
            //调用123接口（视频）--3.创建摄像点
            //http://121.40.140.34/xingjiansport/V2/XiaoAi/createCameraPoint?raceid=31&cameraid=15&longitude=121.363839&latitude=31.161295
            var raceid = vm.camera_id;
            var baseurl = "http://www.123yd.cn/xingjiansport/V2/XiaoAi/createCameraPoint?raceid=" + raceid + "&cameraid=" + id + "&longitude=0&latitude=0";
            $.get(baseurl, {}, function (data2) {
                if (data2.errorcode != 0) {
                    vm.dialogService.error(data2.errormsg);
                } else {
                    console.log('创建成功--' + raceid + '--' + id);
                    console.log(data2);
                }
            }, 'json');
        }


    }

    // 表单提交
    submitForm(isValid) {
        var vm = this;
        // 验证失败, 提示用户
        if (!isValid) {
            
            if (!vm.cp.cp_name) {
                $.blink(angular.element(document.getElementsByName('cpName')), 5, 100)
            }

        }

        // 验证通过
        if (isValid) {
            // console.log(vm.deviceList.data);
            
            var mid = '';
            angular.forEach(vm.bindEquipment, function (element, index) {
                mid += element + ',';
            });
            console.log(mid.substring(0, mid.length - 1));

            // 拼接POST数据格式
            var params = {
                cp_index: vm.cp.cp_index,
                cp_name: vm.cp.cp_name,
                mid: mid.substring(0, mid.length - 1),
                is_camera_point: vm.isLiveVideo ? 'yes' : 'no',
                is_timing_point: vm.isTimingPoint ? 'yes' : 'no'
            }


            // 新增计时点
            vm.CpService.postCpCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {
                // 成功调用
                if (data.code==0) {
                    // console.log(vm.camera_id + '---123');
                    // 选择为摄像点的场合
                    if (vm.isLiveVideo && vm.camera_id != 0) {
                        // 循环创建cameraid(123)
                        angular.forEach(vm.deviceList.data, function(data, index, array) {

                            angular.forEach(vm.bindEquipment, function(data2, index2, array2) {
                                if (data2 == data.mid && data.type == "camera") {
                                    // 创建摄像点
                                    vm.create_camera_id(data.name);
                                }
                            });

                        });
                    }
                }
                data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.detail');


                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);

                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }
}

CpController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'CpService', 'getCpList', 'iRunner', 'getDeviceList']; 

export default CpController;